package com.shisan.tingshu.account.service.impl;

import com.shisan.tingshu.account.mapper.RechargeInfoMapper;
import com.shisan.tingshu.account.service.RechargeInfoService;
import com.shisan.tingshu.common.constant.SystemConstant;
import com.shisan.tingshu.common.util.AuthContextHolder;
import com.shisan.tingshu.model.account.RechargeInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shisan.tingshu.vo.account.RechargeInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	/**
	 * 零钱充值
	 * @param rechargeInfoVo
	 * @return
	 */
	@Override
	public Map<String, Object> submitRecharge(RechargeInfoVo rechargeInfoVo) {

		RechargeInfo rechargeInfo = new RechargeInfo();
		rechargeInfo.setUserId(AuthContextHolder.getUserId());
		String orderNo = RandomStringUtils.random(15, false, true);
		rechargeInfo.setOrderNo(orderNo);
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
		int insert = rechargeInfoMapper.insert(rechargeInfo);
		log.info("保存零钱订单状态：{}", insert > 0 ? "success" : "fail");
		Map<String, Object> map = new HashMap<>();
		map.put("orderNo", orderNo);
		return map;
	}
}
